package com.aaa.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/7/5 9:28
 * @description：这辈子第一个拦截器
 * @modified By：
 * @version: 1.0
 */
@Component
public class MyInterceptor  extends HandlerInterceptorAdapter {
    StopWatch stopWatch1= new StopWatch("chenjian");
    StopWatch stopWatch2= new StopWatch("zhangsgan");
    /**
     * 类似前置增强处理
     * controller中的方法执行之前使用
     * @param request
     * @param response
     * @param handler
     * @return  如果返回true那么就继续往下走，第一种直接进入到controller的方法中，第二种进入到下一个拦截器
     *  如果返回的是false，直接停止，不仅如此下一个拦截器或者controller中的方法。
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle执行啦");
        stopWatch1.start();
        return true;
    }
    /**
     * 类似后置增强处理
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle执行啦");
        stopWatch1.stop();
        System.out.println("花费时间："+stopWatch1.getTotalTimeMillis());
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion执行啦");
    }
}
